#!/usr/bin/make -f
# This file has gone through many maintainers.  Mostly rewritten
# by Daniel Jacobowitz.

CFLAGS := $(shell dpkg-buildflags --get CFLAGS)
export CXXFLAGS := $(shell dpkg-buildflags --get CXXFLAGS)
export LDFLAGS := $(shell dpkg-buildflags --get LDFLAGS)

# These are important since this is a library package
soname=5
sodepver = (>= 5.7+20100313)

# Name our packages
package-base=ncurses-base
package-bin=ncurses-bin
package-lib=libncurses$(soname)
package-lib-32=lib32ncurses$(soname)
package-lib-64=lib64ncurses$(soname)
package-libw=libncursesw$(soname)
package-libw-32=lib32ncursesw$(soname)
package-dev=libncurses$(soname)-dev
package-dev-32=lib32ncurses$(soname)-dev
package-dev-64=lib64ncurses$(soname)-dev
package-devw=libncursesw$(soname)-dev
package-devw-32=lib32ncursesw$(soname)-dev
package-dbg=libncurses$(soname)-dbg
package-dbgw=libncursesw$(soname)-dbg
package-term=ncurses-term
package-examples=ncurses-examples

workdir=$(shell pwd)
tempdir=debian/tmp
fulltempdir=$(workdir)/$(tempdir)

srcdir=$(shell pwd)
objdir=$(srcdir)/obj
objdir-static=$(srcdir)/obj-static
objdir-32=$(srcdir)/obj-32
objdir-64=$(srcdir)/obj-64
wobjdir=$(srcdir)/obj-wide
wobjdir-static=$(srcdir)/obj-wide-static
wobjdir-32=$(srcdir)/obj-wide-32
objdir-debug=$(srcdir)/obj-debug
wobjdir-debug=$(srcdir)/obj-wide-debug
objdir-test=$(srcdir)/obj-test

DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_HOST_GNU_SYSTEM ?= $(shell dpkg-architecture -qDEB_HOST_GNU_SYSTEM)
DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

autogen-files := debian/libncurses5-dev.links debian/libncursesw5-dev.links \
		 debian/libncurses5.install debian/libncursesw5.install \
		 debian/libncurses5-dev.install debian/libncursesw5-dev.install

ifeq ($(DEB_HOST_ARCH),i386)
build_64_target = x86_64-$(DEB_HOST_GNU_SYSTEM)
build_64 = build-64
endif

ifeq ($(DEB_HOST_ARCH),powerpc)
build_64_target = powerpc64-$(DEB_HOST_GNU_SYSTEM)
build_64 = build-64
endif

ifeq ($(DEB_HOST_ARCH),s390)
build_64_target = s390x-$(DEB_HOST_GNU_SYSTEM)
build_64 = build-64
endif

ifeq ($(DEB_HOST_ARCH),sparc)
build_64_target = sparc64-$(DEB_HOST_GNU_SYSTEM)
build_64 = build-64
endif

ifeq ($(DEB_HOST_ARCH),amd64)
build_32_target = i486-$(DEB_HOST_GNU_SYSTEM)
build_32 = build-32 build-wide-32
lib32 = /lib32
usr_lib32 = /usr/lib32
endif

ifeq ($(DEB_HOST_ARCH),ppc64)
build_32_target = powerpc-$(DEB_HOST_GNU_SYSTEM)
build_32 = build-32 build-wide-32
lib32 = /lib32
usr_lib32 = /usr/lib32
endif

ifneq ($(findstring linux,$(DEB_HOST_GNU_SYSTEM)),)
with_gpm = --with-gpm
endif

CONFARGS =	--prefix=/usr \
		--with-shared \
		--mandir=/usr/share/man \
		--without-profile --without-debug \
		--disable-rpath --enable-echo \
		--enable-const \
		--enable-pc-files \
		--without-ada \
		--without-tests \
		--without-progs \
		$(with_gpm) \
		--enable-symlinks \
		--disable-lp64 \
		--with-chtype='long' \
		--with-mmask-t='long' \
		--disable-termcap \
		--with-default-terminfo-dir=/usr/share/terminfo \
		--with-terminfo-dirs="/etc/terminfo:/lib/terminfo:/usr/share/terminfo" \
		--with-ticlib

CONFARGS-TEST =	--prefix=/usr \
		--bindir=/usr/lib/ncurses/examples \
		--with-curses-dir=$(objdir)

ifneq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
CONFARGS += --with-build-cc=gcc --host=$(DEB_HOST_GNU_TYPE)
CONFARGS-TEST += --with-build-cc=gcc --host=$(DEB_HOST_GNU_TYPE)
endif

# Files for /lib/terminfo.
libterminfofiles=a/ansi c/cons25 c/cons25-debian c/cygwin d/dumb h/hurd l/linux m/mach \
	m/mach-bold m/mach-color p/pcansi r/rxvt r/rxvt-m r/rxvt-basic \
	r/rxvt-unicode \
	s/screen s/screen-bce s/screen-s s/screen-w s/sun v/vt100 v/vt102 \
	v/vt220 v/vt52 x/xterm x/xterm-debian x/xterm-xfree86 x/xterm-color \
	x/xterm-mono x/xterm-r5 x/xterm-r6 x/xterm-vt220 \
	x/xterm-256color s/screen-256color s/screen-256color-bce \
	w/wsvt25 w/wsvt25m E/Eterm E/Eterm-color

# Blacklist terminfo entries shipped by other Debian packages so that we do
# not begin shipping them by accident.
badterminfo = 5/5250 b/bterm i/iterm i/iterm-am i/iterm-color \
	j/jfbterm k/kon k/kon2 l/linux-5250

# The ncurses configure test won't work when cross compiling (e.g. for
# 64 bit libraries), so force the libgpm soname to the correct value.
export cf_cv_gpm_soname=libgpm.so.2

$(objdir)/config.status:
	test -d $(objdir) || mkdir $(objdir)
	cd $(objdir) && CFLAGS="$(CFLAGS)" $(srcdir)/configure \
		$(CONFARGS) \
		--with-progs \
		--with-install-prefix=$(fulltempdir) \
		--enable-overwrite --libdir=/usr/lib/$(DEB_HOST_MULTIARCH)

$(objdir-static)/config.status:
	test -d $(objdir-static) || mkdir $(objdir-static)
	cd $(objdir-static) && CFLAGS="$(CFLAGS)" $(srcdir)/configure \
		$(CONFARGS) \
		--without-shared \
		--without-dlsym \
		--with-install-prefix=$(fulltempdir) \
		--enable-overwrite --libdir=/usr/lib/$(DEB_HOST_MULTIARCH)

$(objdir-32)/config.status:
	test -d $(objdir-32) || mkdir $(objdir-32)

	cd $(objdir-32) && CFLAGS="$(CFLAGS)" CC="gcc -m32" \
		BUILD_CC="gcc" CXX="g++ -m32" \
		$(srcdir)/configure \
		$(CONFARGS) \
		--build=$(DEB_BUILD_GNU_TYPE) \
		--host=$(build_32_target) \
		--without-gpm \
		--with-install-prefix=$(fulltempdir) \
		--enable-overwrite --libdir=$(usr_lib32)

$(objdir-64)/config.status:
	test -d $(objdir-64) || mkdir $(objdir-64)

	# Force necessary configure checks which do not work when
	# cross compiling; all Debian platforms return unsigned char
	# for this configure check.
	cf_cv_type_of_bool='unsigned char'; export cf_cv_type_of_bool; \
	cd $(objdir-64) && CFLAGS="$(CFLAGS)" CC="gcc -m64" \
		BUILD_CC="gcc" CXX="g++ -m64" \
		$(srcdir)/configure \
		$(CONFARGS) \
		--build=$(DEB_BUILD_GNU_TYPE) \
		--host=$(build_64_target) \
		--without-gpm \
		--with-install-prefix=$(fulltempdir) \
		--enable-overwrite --libdir=/usr/lib64

$(objdir-debug)/config.status:
	test -d $(objdir-debug) || mkdir $(objdir-debug)
	cd $(objdir-debug) && \
		CFLAGS="$(CFLAGS) -DTRACE" $(srcdir)/configure \
		$(CONFARGS) \
		--with-install-prefix=$(fulltempdir) \
		--enable-overwrite --libdir=/usr/lib/$(DEB_HOST_MULTIARCH)

$(wobjdir)/config.status:
	test -d $(wobjdir) || mkdir $(wobjdir)
	cd $(wobjdir) && CFLAGS="$(CFLAGS)" $(srcdir)/configure \
		$(CONFARGS) \
		--with-install-prefix=$(fulltempdir) \
		--disable-overwrite --enable-widec \
		--libdir=/usr/lib/$(DEB_HOST_MULTIARCH)

$(wobjdir-static)/config.status:
	test -d $(wobjdir-static) || mkdir $(wobjdir-static)
	cd $(wobjdir-static) && CFLAGS="$(CFLAGS)" $(srcdir)/configure \
		$(CONFARGS) \
		--without-shared \
		--without-dlsym \
		--with-install-prefix=$(fulltempdir) \
		--disable-overwrite --enable-widec \
		--libdir=/usr/lib/$(DEB_HOST_MULTIARCH)

$(wobjdir-debug)/config.status:
	test -d $(wobjdir-debug) || mkdir $(wobjdir-debug)
	cd $(wobjdir-debug) && \
		CFLAGS="$(CFLAGS) -DTRACE" $(srcdir)/configure \
		$(CONFARGS) \
		--with-install-prefix=$(fulltempdir) \
		--disable-overwrite --enable-widec \
		--libdir=/usr/lib/$(DEB_HOST_MULTIARCH)

$(wobjdir-32)/config.status:
	test -d $(wobjdir-32) || mkdir $(wobjdir-32)

	cd $(wobjdir-32) && CFLAGS="$(CFLAGS)" CC="gcc -m32" \
		BUILD_CC="gcc" BUILD_CPPFLAGS="-D_GNU_SOURCE" CXX="g++ -m32" \
		$(srcdir)/configure \
		$(CONFARGS) \
		--build=$(DEB_BUILD_GNU_TYPE) \
		--host=$(build_32_target) \
		--without-gpm \
		--with-install-prefix=$(fulltempdir) \
		--disable-overwrite --enable-widec \
		--libdir=$(usr_lib32)

$(objdir-test)/config.status: build-normal
	test -d $(objdir-test) || mkdir $(objdir-test)
	cd $(objdir-test) && CFLAGS="$(CFLAGS)" $(srcdir)/test/configure \
		$(CONFARGS-TEST)

build-arch build-indep: build

build: build-normal build-static build-wide build-debug build-wide-debug \
	build-wide-static build-test $(build_64) $(build_32)

	touch $@

build-normal: $(objdir)/config.status
	cd $(objdir) && $(MAKE)
	touch $@

build-static: $(objdir-static)/config.status
	cd $(objdir-static) && $(MAKE)
	touch $@

build-32: $(objdir-32)/config.status
	cd $(objdir-32) && $(MAKE)
	touch $@

build-64: $(objdir-64)/config.status
	cd $(objdir-64) && $(MAKE)
	touch $@

build-debug: $(objdir-debug)/config.status
	cd $(objdir-debug) && $(MAKE)
	touch $@

build-wide: $(wobjdir)/config.status
	cd $(wobjdir) && $(MAKE)
	touch $@

build-wide-static: $(wobjdir-static)/config.status
	cd $(wobjdir-static) && $(MAKE)
	touch $@

build-wide-32: $(wobjdir-32)/config.status
	cd $(wobjdir-32) && $(MAKE)
	touch $@

build-wide-debug: $(wobjdir-debug)/config.status
	cd $(wobjdir-debug) && $(MAKE)
	touch $@

build-test: $(objdir-test)/config.status
	cd $(objdir-test) && $(MAKE)
	touch $@

clean:
	dh_testdir
	rm -f build
	rm -f build-normal build-static build-wide build-debug build-wide-debug \
		build-wide-static build-test $(build_32) $(build_64)
	rm -rf $(objdir)
	rm -rf $(objdir-static)
	rm -rf $(objdir-32)
	rm -rf $(objdir-64)
	rm -rf $(wobjdir)
	rm -rf $(wobjdir-static)
	rm -rf $(wobjdir-32)
	rm -rf $(objdir-debug)
	rm -rf $(wobjdir-debug)
	rm -rf $(objdir-test)
	rm -f $(autogen-files)
	dh_clean

install: build
	# Generate proper debhelper files for so-named packages
	dh_testdir
	dh_testroot
	dh_prep

	# Static debug libraries need _g suffix to avoid file clashes
	for lib in $(objdir-debug)/lib/*.a; do \
	  cp -p $$lib `echo $$lib | sed 's/\.a$$/_g.a/'`; \
	done
	for lib in $(wobjdir-debug)/lib/*.a; do \
	  cp -p $$lib `echo $$lib | sed 's/\.a$$/_g.a/'`; \
	done

	# Install non -DTRACE versions.
	GZIP='-9' $(MAKE) -C $(objdir) install
	$(MAKE) -C $(wobjdir) install.libs

	install -m 644 -t $(fulltempdir)/usr/lib/$(DEB_HOST_MULTIARCH) $(objdir-static)/lib/*.a
	install -m 644 -t $(fulltempdir)/usr/lib/$(DEB_HOST_MULTIARCH) $(wobjdir-static)/lib/*.a

	# Remove blacklisted terminal types and other stuff we don't install
	cd $(tempdir)/usr/share/terminfo && rm -f $(badterminfo)
	rm -f $(tempdir)/usr/lib/terminfo $(tempdir)/usr/lib/$(DEB_HOST_MULTIARCH)/libcurses.so

	# Compile Debian-specific terminfo entries before copying files
	# into their packages.  Hurd uses gsbom/grbom, so pass -x to tic.
ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
	for ti in xterm hurd rxvt rxvt-unicode; do \
	  LD_LIBRARY_PATH=$${LD_LIBRARY_PATH}:$(fulltempdir)/usr/lib:$(fulltempdir)/lib \
		TERMINFO=$(fulltempdir)/usr/share/terminfo \
		$(tempdir)/usr/bin/tic -x debian/$$ti.ti; \
	done
else
	@build_ver=`sed -n -e					\
	  's/^.*NCURSES_VERSION_STRING \"\(.*\)\"$$/\1/p'	\
	  obj/include/ncurses_cfg.h`;				\
	if [ "$$build_ver" != `tic -V | sed 's/^.* //'` ]; then	\
	  echo "Build environment version mismatch for tic!";	\
	  exit 1;						\
	fi

	for ti in xterm hurd rxvt rxvt-unicode; do \
		TERMINFO=$(fulltempdir)/usr/share/terminfo \
		tic -x debian/$$ti.ti; \
	done

endif

	# Move ncurses-base terminfo entries to /lib/terminfo.
	for f in $(libterminfofiles); do \
		dir=$(tempdir)/lib/terminfo/$$(dirname $$f); \
		mkdir -p $$dir; \
		mv $(tempdir)/usr/share/terminfo/$$f $$dir; \
	done

	# Create an empty /etc/terminfo for the sysadmin.
	mkdir -p $(tempdir)/etc/terminfo
	install --mode 644 --owner=root --group=root \
		debian/README.etc $(tempdir)/etc/terminfo/README

	# Install the test programs, sparing the detour to debian/tmp.
	$(MAKE) -C $(objdir-test) DESTDIR=$(workdir)/debian/$(package-examples) install

debian/%: debian/%.in
	sed -e 's/$${DEB_HOST_MULTIARCH}/$(DEB_HOST_MULTIARCH)/g' $< > $@

binary:	binary-arch binary-indep

binary-arch: build install $(autogen-files)
	dh_testdir
	dh_testroot
	dh_install -s --fail-missing
	dh_installman -s
	dh_installdocs -s debian/FAQ
	dh_installchangelogs -s NEWS
	dh_installdirs -s

	# Strip the packages, shipping detached debugging symbols.
	dh_strip -s -N$(package-dbg) -N$(package-dbgw) -N$(package-libw) \
		--dbg-package=$(package-dbg)
	dh_strip -p$(package-libw) --dbg-package=$(package-dbgw)
	dh_lintian -s
	dh_compress -p$(package-examples) usr/lib/ncurses/examples/README
	dh_compress -s -N$(package-examples)
	dh_fixperms -s
	dh_link -s
	dh_makeshlibs -p$(package-lib) -V "$(package-lib) $(sodepver)" -- -c4
	dh_makeshlibs -p$(package-libw) -V "$(package-libw) $(sodepver)" -- -c4
ifneq ($(build_32),)
	dh_makeshlibs -p$(package-lib-32) -V "$(package-lib-32) $(sodepver)" -- -c4
	dh_makeshlibs -p$(package-libw-32) -V "$(package-libw-32) $(sodepver)" -- -c4
endif
ifneq ($(build_64),)
	dh_makeshlibs -p$(package-lib-64) -V "$(package-lib-64) $(sodepver)" -- -c4
endif
	dh_shlibdeps -s
	dh_gencontrol -s
	dh_installdeb -s
	dh_md5sums -s
	dh_builddeb -s

binary-indep: build install
	dh_testdir
	dh_testroot

	dh_installdocs -i
	dh_installchangelogs -i NEWS
	dh_install -i --fail-missing
	dh_lintian -i
	dh_compress -i
	dh_fixperms -i
	dh_link -i
	dh_gencontrol -i
	dh_installdeb -i
	dh_md5sums -i
	dh_builddeb -i

.PHONY: binary binary-arch binary-indep clean install
